﻿using System;
using System.Collections.Generic;
using System.Text;
using CFSystem.JXC.IDAL;
using CFSystem.JXC.Model;
using System.Data;
using System.Data.OleDb;
using DBUtility;

namespace CFSystem.JXC.AccessDAL
{
    /// <summary>
    /// 库存异动设置
    /// </summary>
    public class InvTransaction: ITransaction
    {
        #region ITransaction Members

        public IList<InvTransactionData> GetInvTransHistoryInfo()
        {
            string sqlSelect = "Select * from [tb_InvChange]";

            OleDbDataReader dr = AccessHelper.ExecuteReader(AccessHelper.ConnectionString, CommandType.Text, sqlSelect);

            IList<InvTransactionData> Items = new List<InvTransactionData>();

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Items.Add(GetChangeHisInfo(dr));
                }
                dr.Close();
            }
            return Items;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private InvTransactionData GetChangeHisInfo(OleDbDataReader dr)
        {
            InvTransactionData itd = new InvTransactionData();
            itd.ChangeID = Convert.ToInt32(dr["ChangedID"]);
            itd.ChangeReasonID = Convert.ToInt32(dr["ReasonID"]);
            itd.ChangeCount = Convert.ToInt32(dr["count"]);
            itd.ChangeDate = Convert.ToDateTime(dr["date"]);
            itd.ProductID = dr["GoodID"].ToString();
            itd.ProductName = dr["GoodName"].ToString();
            itd.Memo = dr["memo"].ToString();
            return itd;
        }

        public InvTransactionData GetInvTransRecord(int InvTransID)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 新增库存异动记录
        /// </summary>
        /// <param name="InvTransInfo"></param>
        public void InsertInvTransRecord(InvTransactionData InvTransInfo)
        {
            string sqlInsert = "insert into [tb_InvChange]([ReasonID],[GoodID],[GoodName],[Count],[Date],[ops],[memo]) values('" + InvTransInfo.ChangeReasonID + "', '"
                                         + InvTransInfo.ProductID + "','" + InvTransInfo.ProductName + "','"
                                         + InvTransInfo.ChangeCount + "'," + InvTransInfo.ChangeDate.ToShortDateString() + "," 
                                         +"'jackie'" + ",'" + InvTransInfo.Memo +"')";
                                        
            int returnCnt = AccessHelper.ExecuteNonQuery(AccessHelper.ConnectionString, CommandType.Text, sqlInsert);
            
        }

        public void DelInvTransRecordByID(int InTransID)
        {
            string sqlInsert = "Delete from  [tb_InvChange] where [ChangedID] =" + InTransID;

            int returnCnt = AccessHelper.ExecuteNonQuery(AccessHelper.ConnectionString, CommandType.Text, sqlInsert);
        }

        #endregion
    }
}
